import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from "vuex-persist";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    carList: [],
  },
  mutations: {
    add(state, item) {//加入购物车
      var flag = false;
      state.carList.forEach((i) => {
        if (i.product_id == item.product_id) {
          flag = true;
          i.num++;
        }
      });
      if (flag == false) {
        Vue.set(item, "num", 1);
        state.carList.push(item);
      }
    },
    pedometer() { },//计步器

    remove(state, id) {//删除商品
      state.carList.forEach((item, index) => {
        if (item.product_id == id) {
          state.carList.splice(index, 1);
        }
      })
    },
 



  },

  actions: {
  },
  modules: {
  },
  plugins: [
    new VuexPersistence({
      storage: window.localStorage,
    }).plugin,
  ],
})
